package com.kyzh.core.activities.weal.gift

import android.content.Context
import android.content.Intent
import android.content.res.ColorStateList
import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import androidx.core.widget.addTextChangedListener
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.gushenge.core.beans.GameGift
import com.gushenge.core.beans.Gift
import com.gushenge.core.impls.GameImpl
import com.gushenge.core.listeners.ResultListener
import com.gushenge.core.requests.GameRequest
import com.gushenge.core.requests.UserRequest
import com.gushenge.core.toast
import com.kyzh.core.R
import com.gushenge.core.base.activity.BaseActivity
import com.kyzh.core.databinding.DialogGiftBinding
import com.kyzh.core.databinding.ItemDialogGiftBinding
import com.kyzh.core.databinding.ItemGiftNewBinding
import com.kyzh.core.utils.copy
import com.kyzh.core.utils.setVisibility
import kotlinx.android.synthetic.main.activity_gift.etSearch
import kotlinx.android.synthetic.main.activity_gift.ivClose
import kotlinx.android.synthetic.main.activity_gift_new.*
import org.jetbrains.anko.support.v4.onRefresh


class GiftActivity : BaseActivity() {
    companion object{
        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context, GiftActivity::class.java)
            context.startActivity(starter)
        }
    }
    private var mp = 1
    private val adapter = Adapter()
    private val hander  = Handler() {
        if(it.what ==1){
            getSearch(it.obj.toString() ,1)
        }
        true
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_gift_new)

        initView()
    }

    private fun initView() {
        rev.layoutManager = GridLayoutManager(this,2)
        rev.adapter = adapter
        etSearch.addTextChangedListener {
            mp =1
            if(it.isNullOrBlank()){
                getSearch(it.toString().trim(),1)
            }else{
                hander.removeMessages(1)
                val obtainMessage = hander.obtainMessage()
                obtainMessage.obj = it
                obtainMessage.what = 1
                hander.sendMessageAtTime(obtainMessage,1000)
            }
        }
        ivClose.setOnClickListener {
            etSearch.setText("")
        }
        getSearch("",1)
        swipe.onRefresh {
            mp =1
            getSearch("",1)
        }
        adapter.loadMoreModule.setOnLoadMoreListener {
            mp ++
            getSearch("",mp)
        }

        adapter.setEmptyView(R.layout.empty)


        adapter.setOnItemClickListener{a,v,p->
            val any = a.data[p] as GameGift


            val bottomSheetDialog = BottomSheetDialog(this, R.style.BottomSheetDialog)
            val view = getView(any.gid,bottomSheetDialog)
            bottomSheetDialog.setContentView(view)
            bottomSheetDialog.show()
        }
    }




    private fun getSearch(s: String = "",mp:Int) {
        UserRequest.giftCenterNew(s,type = 0,mp){ p, max_p, data->
            swipe.isRefreshing = false
            if(mp==1){
                adapter.setNewInstance(data)
            }else{
                adapter.addData(data)
            }
            adapter.loadMoreModule.loadMoreComplete()
            if(mp>=max_p){
                adapter.loadMoreModule.loadMoreEnd(true)
            }
        }
    }


    private fun getView(gid:String,v:BottomSheetDialog)=DialogGiftBinding.inflate(layoutInflater).apply {
        progress.setVisibility(true)
        group.setVisibility(false)
        val itemAdapter = ItemAdapter()
        rev.layoutManager = LinearLayoutManager(this@GiftActivity)
        rev.adapter = itemAdapter
        val db = this
        cancel.setOnClickListener {
            v.dismiss()
        }
        itemAdapter.setOnItemChildClickListener{a,v,p->
            val gift = a.data[p] as Gift
            if(v.id==R.id.tvGet){
                if(gift.card.isNullOrBlank()){
                    if(gift.shengyu>1){
                        //请求
                        GameImpl.getGift(gift.id,object : ResultListener {
                            override fun success(bean: Any) {
                                 bean as String
                                bean.copy()
                                "您的礼包码 $bean 已复制".toast()
                                GameRequest.gameGift(gid,"0"){
                                    db.data = this
                                    progress.setVisibility(false)
                                    group.setVisibility(true)
                                    itemAdapter.setNewInstance(this.list)
                                }
                            }

                            override fun error(error: String) {
                                error.toast()
                            }
                        })

                    }
                }else{
                    gift.card.copy()
                    "您的礼包码 ${ gift.card} 已复制".toast()
                }
            }
        }
        GameRequest.gameGift(gid,"0"){
            db.data = this
            progress.setVisibility(false)
            group.setVisibility(true)
            itemAdapter.setNewInstance(this.list)
        }
    }.root

    private inner class ItemAdapter : BaseQuickAdapter<Gift,BaseDataBindingHolder<ItemDialogGiftBinding>>(R.layout.item_dialog_gift){
        init {
            addChildClickViewIds(R.id.tvGet)
        }
        override fun convert(holder: BaseDataBindingHolder<ItemDialogGiftBinding>, item: Gift) {
            holder.dataBinding?.apply {
                data=item
                tips.text = "剩余${item.bfb}%"
                progress.progress = 100- item.bfb


                if(item.card.isNullOrBlank()){
                    if(item.shengyu<1){
                        tvGet.text = "已领完"
                        tvGet.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#EEEEEE"))
                        tvGet.setTextColor(Color.parseColor("#CCCCCC"))
                        tvGet.isEnabled = false
                    }else{
                        tvGet.text = "领取"
                        tvGet.backgroundTintList = null
                        tvGet.setTextColor(Color.WHITE)
                        tvGet.isEnabled = true
                    }
                }else{
                    tvGet.isEnabled = true
                    tvGet.text = "复制"
                    tvGet.backgroundTintList = null
                    tvGet.setTextColor(Color.WHITE)
                }
            }
        }
    }

    private inner  class Adapter :BaseQuickAdapter<GameGift,BaseDataBindingHolder<ItemGiftNewBinding>>(R.layout.item_gift_new),LoadMoreModule{
        override fun convert(holder: BaseDataBindingHolder<ItemGiftNewBinding>, item: GameGift) {
            holder.dataBinding?.data=item
        }

    }

}